<?php

namespace app\admin\model;

use think\Model;

class Location extends Model {

        protected function _before_write(&$data) {
                parent::_before_write($data);
                if (!empty($data['cid'])) {
                        $data['cid'] = $data['cid'] ? "," . implode(",", $data['cid']) . "," : "";
                } else {
                        $data['cid'] = '';
                }
        }

        protected function _after_find(&$result, $options = array()) {
                parent::_after_find($result, $options);
                if (!empty($result['cid'])) {
                        $result['cid'] = array_filter(explode(",", $result['cid']));
                } else {
                        $result['cid'] = array();
                }
        }

        protected function _after_select(&$resultSet, $options = array()) {
                parent::_after_select($resultSet, $options);
                foreach ($resultSet as $key => $value) {
                        $child_num = $this->where(array('pid' => $value['id']))->count();
                        $resultSet[$key]['child_num'] = $child_num;
                }
        }

}
